﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Net.Mail;
using System.Configuration;
using TripsLogic.controller;
using TripsLogic.model;
using System.Xml;
using System.Text;

namespace TripsLogic.util
{
    public static class Common
    {
        public static string strDateTimeToString(string strDateTime)
        {
            strDateTime = strDateTime.Replace("/", "");
            strDateTime = strDateTime.Replace("-", "");
            strDateTime = strDateTime.Replace(" ", "");
            strDateTime = strDateTime.Replace(":", "");
            return strDateTime;
        }

        public static string getRandomString(int size)
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString().ToLower();
        }
        public static string generateHash(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            Byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
            bs = x.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (Byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            string outstr = s.ToString();
            return outstr;
        }

        public static DateTime strToFormatDateTime(string strDateTime, string dtFormat)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            return DateTime.ParseExact(strDateTime, dtFormat, provider);
        }

        public static DateTime parseDate(string s, string format)
        {
            DateTime result;
            if (!DateTime.TryParse(s, out result))
            {
                result = DateTime.ParseExact(s, format, System.Globalization.CultureInfo.InvariantCulture);
            }
            return result;
        }

        public static DateTime strToDateTime(string strDateTime)
        {
            CultureInfo provider = CultureInfo.InvariantCulture;
            return DateTime.ParseExact(strDateTime.Trim(), "yyyyMMddHHmmss", provider);
        }

        public static string getComicRate(string rate)
        {
            double dRate = double.Parse(rate);
            if (dRate <= 1)
                return "0.png";
            if (dRate < 2)
                return "1.png";
            if (dRate < 3)
                return "2.png";
            if (dRate < 4)
                return "3.png";
            if (dRate < 5)
                return "4.png";
            if (dRate >= 5)
                return "5.png";
            return "0.png";
        }


        public static string getRate(string rate)
        {
            double dRate = double.Parse(rate);
            if (dRate < 0.5)
                return "iStar";
             if (dRate <1)
                return "iStar i_s0-5";
            if (dRate <1.5)
                return "iStar i_s1";
            if (dRate <2)
                return "iStar i_s1-5";
            if (dRate <2.5)
                return "iStar i_s2";
            if (dRate <3)
                return "iStar i_s2-5";
            if (dRate <3.5)
                return "iStar i_s3";
            if (dRate <4)
                return "iStar i_s3-5";
            if (dRate <4.5)
                return "iStar i_s4";
            if (dRate <5)
                return "iStar i_s4-5";
            if (dRate >=5)
                return "iStar i_s5";
             return "iStar";
        }

        public static string getLikeRate(string likeRate)
        {
            double dLikeRate = double.Parse(likeRate);
            if (dLikeRate < 0.5)
               return "iHeart";
            if (dLikeRate <1)
               return "iHeart i_h0-5";
            if (dLikeRate <1.5)
               return "iHeart i_h1";
            if (dLikeRate <2)
               return "iHeart i_h1-5";
            if (dLikeRate <2.5)
                return "iHeart i_h2";
            if (dLikeRate <3)
                return "iHeart i_h2-5";
            if (dLikeRate <3.5)
                return "iHeart i_h3";
            if (dLikeRate <4)
                return "iHeart i_h3-5";
            if (dLikeRate <4.5)
                return "iHeart i_h4";
            if (dLikeRate <5)
                return "iHeart i_h4-5";
            if (dLikeRate >=5)
                return "iHeart i_h5";
            return "iHeart";
        }

        public static bool sendEmail(string strTo, string strFrom, string strCc, string strBcc, string strSubject, string strBody, string strAttPath)
        {
            try
            {
                MailMessage mail = new MailMessage();

                string strUsername = ConfigurationManager.AppSettings["smtpUser"].ToString();
                string strPassword = ConfigurationManager.AppSettings["smtpPassword"].ToString();

                int iValue = 700;

                if (strTo == "" && strTo != null)
                {
                    //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), "Empty To-address", 1, "Error sendEmail"));
                    return false;
                }

                string[] arrStrTo = strTo.Split(';');
                foreach (string strItem in arrStrTo)
                {
                    mail.To.Add(strItem.Trim());
                }

                if (strCc != "" && strCc != null)
                {
                    string[] arrStrCc = strCc.Split(';');
                    foreach (string strItem in arrStrCc)
                    {
                        mail.CC.Add(strItem.Trim());
                    }
                }

                if (strBcc != "" && strBcc != null)
                {
                    string[] arrStrBcc = strBcc.Split(';');
                    foreach (string strItem in arrStrBcc)
                    {
                        mail.Bcc.Add(strItem.Trim());
                    }
                }

                if (strSubject == "")
                    strSubject = "(No subject)";
                else
                    mail.Subject = strSubject;
                mail.Body = HttpUtility.HtmlDecode(strBody);
                mail.IsBodyHtml = true;

                if (strAttPath != "" && strAttPath != null)
                {
                    string[] arrStrAttPath = strAttPath.Split(';');
                    foreach (string strItem in arrStrAttPath)
                    {
                        Attachment attachment = new Attachment(strItem.Trim());
                        mail.Attachments.Add(attachment);
                    }
                }

                if (strFrom == "")
                {
                    strFrom = strUsername;
                }

                mail.From = new MailAddress(strFrom);

                string[] smtpConfig = ConfigurationManager.AppSettings["smtpServer"].Split(';');

                string strSmtpServer = Convert.ToString(smtpConfig.GetValue(0));
                int iSmtpPort = Convert.ToInt32(smtpConfig.GetValue(1));
                bool isAuthenticated = Convert.ToBoolean(smtpConfig.GetValue(2));
                bool isSSLEnable = Convert.ToBoolean(smtpConfig.GetValue(3));

                SmtpClient SmtpServer = new SmtpClient(strSmtpServer);

                SmtpServer.Port = iSmtpPort;
                if (isAuthenticated)
                    SmtpServer.Credentials = new System.Net.NetworkCredential(strUsername, strPassword);
                SmtpServer.EnableSsl = isSSLEnable;
                SmtpServer.Send(mail);

                return true;
            }
            catch (Exception e)
            {
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), e.ToString(), 1, "Error sendEmail " + e.InnerException == null ? e.InnerException.ToString() : ""));
                return false;

            }
        } 
    }
}